//
//  NetWorkBaseRequest.h
//  Andy
//
//  Created by mac on 15/7/16.
//  Copyright (c) 2015年 Andy. All rights reserved.
//


#import "NetWorkBaseRequest.h"
#import "Digest.h"

@implementation NetWorkBaseRequest

- (instancetype)init
{
    self = [super init];
    if (self) {
        //内部测试服务器：@"https://192.168.1.100:1443/EChineseMedicine/rs"
        //阿里云服务器 ：@"https://open.ezhongyi591.com/EChineseMedicine/rs"
        
        //默认请求地址、默认请求模块
        self.apiDomain = @"https://192.168.1.100:1443/EChineseMedicine/rs";
                
        self.paramDic = [[NSMutableDictionary alloc] init];
    }
    return self;
}

- (void)setValue:(id)value forKey:(NSString *)key
{
    if (value) {
        [self.paramDic setObject:value forKey:key];
    }
}

- (NSString *)stringOfApiURLString
{
    return [NSString stringWithFormat:@"%@/%@/%@",self.apiDomain,self.moduleName,self.methodName];
}

- (NSString *)getPostBody
{
    NSData *data = [NSJSONSerialization dataWithJSONObject:self.paramDic options:NSJSONWritingPrettyPrinted error:nil];
    NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    return string;
}

- (NSString *)stringOfRandom
{
    int NUMBER_OF_CHARS = 16;
    char data[NUMBER_OF_CHARS];
    for (int x=0;x<NUMBER_OF_CHARS;data[x++] = (char)('A' + (arc4random_uniform(26))));
    return [[NSString alloc] initWithBytes:data length:NUMBER_OF_CHARS encoding:NSUTF8StringEncoding];
}

- (NSString *)getEncrptPostDataString
{
    NSData *data = [NSJSONSerialization dataWithJSONObject:self.paramDic options:0 error:nil];
    NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    
    NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
    NSString *aesKey = [self stringOfRandom];
    NSString *encryptKey = [[Digest sharedInstance] rsaEncryptString:aesKey];
    [dictionary setObject:encryptKey forKey:@"encryptKey"];
    CocoaSecurityResult *result = [CocoaSecurity aesEncryptESPC:string key:aesKey];
    [dictionary setObject:result.base64 forKey:@"data"];
    
    NSData *dataJSONObject = [NSJSONSerialization dataWithJSONObject:dictionary options:0 error:nil];
    NSString *jsonString = [[NSString alloc] initWithData:dataJSONObject encoding:NSUTF8StringEncoding];
    
    return jsonString;
}


@end
